package com.yami.shop.api.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.Museum;
import com.yami.shop.bean.model.MuseumModel;
import com.yami.shop.bean.model.Product;
import com.yami.shop.bean.param.WxMaConfigParam;
import com.yami.shop.bean.vo.MuseumModelVo;
import com.yami.shop.bean.vo.MuseumVo;
import com.yami.shop.personnft.common.model.PersonNft;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.mystery.common.model.MysteryBox;
import com.yami.shop.mystery.common.service.MysteryBoxService;
import com.yami.shop.personnft.common.service.PersonNftService;
import com.yami.shop.service.MuseumModelService;
import com.yami.shop.service.MuseumService;
import com.yami.shop.service.ProductService;
import com.yami.shop.service.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/model/museum")
public class MuseumController {

    @Autowired
    private MuseumService museumService;
    @Autowired
    private MuseumModelService museumModelService;
    @Autowired
    private ProductService productService;
    @Autowired
    private SysConfigService sysConfigService;
    @Autowired
    private MysteryBoxService mysteryBoxService;
    @Autowired
    private PersonNftService personNftService;

    // 首页博物馆列表
    @GetMapping("/list")
    public ServerResponseEntity<IPage<Museum>> getMuseumList(PageParam<Museum> pageParam){
        QueryWrapper<Museum> museumQueryWrapper = new QueryWrapper<>();
        museumQueryWrapper.select("id","museum_img","is_gem","museum_name");
        museumQueryWrapper.eq("status",1);
        museumQueryWrapper.orderByDesc("update_time");
        return ServerResponseEntity.success(museumService.page(pageParam, museumQueryWrapper));
    }

    // 博物管详情
    @GetMapping("/getMuseumData")
    public ServerResponseEntity<MuseumVo> getMuseumData(Long museumId){
        Museum museum = museumService.getById(museumId);
        MuseumVo museumVo = BeanUtil.copyProperties(museum, MuseumVo.class);
        // 获取商品
        QueryWrapper<MuseumModel> museumModelQueryWrapper = new QueryWrapper<>();
        museumModelQueryWrapper.select("id","name","img");
        museumModelQueryWrapper.eq("museum_id",museumId);
        List<MuseumModel> list = museumModelService.list(museumModelQueryWrapper);
        museumVo.setMuseumModelVos(BeanUtil.copyToList(list, MuseumModelVo.class));
        return ServerResponseEntity.success(museumVo);
    }

    // 博物馆商品列表
    @GetMapping("/getMuseumModelList")
    public ServerResponseEntity<IPage<MuseumModel>> getMuseumModelList(PageParam<MuseumModel> pageParam,Long museumId){
        QueryWrapper<MuseumModel> museumModelQueryWrapper = new QueryWrapper<>();
        museumModelQueryWrapper.select("id","name","img");
        museumModelQueryWrapper.eq("museum_id",museumId);
        return ServerResponseEntity.success(museumModelService.page(pageParam,museumModelQueryWrapper));
    }

    // 商品详情
    @GetMapping("/getMuseumModelData")
    public ServerResponseEntity<MuseumModelVo> getMuseumModelData(Long museumModelId,String userId){
        MuseumModel museumModel = museumModelService.getById(museumModelId);
        MuseumModelVo museumModelVo = BeanUtil.copyProperties(museumModel, MuseumModelVo.class);
        // 获取购买链接 是否 拥有持仓
        if(ObjectUtil.isNotEmpty(museumModel.getProdId())){
            Product product = productService.getById(museumModel.getProdId());
            // 获取 购买链接
            if(product.getProdType() == 4){
                // 查询该商品属于那个盲盒
                MysteryBox byId = mysteryBoxService.getById(product.getActivityId());
                museumModelVo.setLinkAddr(byId.getLinkAddr());
            }else{
                museumModelVo.setLinkAddr(product.getLinkAddr());
            }
            // 查询该用户是否有持仓
            if(ObjectUtil.isNotEmpty(userId)){
                List<PersonNft> personNfts = personNftService.listByParam(PersonNft.builder()
                        .userId(userId)
                        .productId(museumModel.getProdId())
                        .build());
                museumModelVo.setIsHave(ObjectUtil.isNotEmpty(personNfts));
            }
        }
        WxMaConfigParam wxMaConfigParam = sysConfigService.getSysConfigObject(Constant.WX_MA_CONFIG, WxMaConfigParam.class);
        museumModelVo.setShowStatus(wxMaConfigParam.getShowStatus());

        return ServerResponseEntity.success(museumModelVo);
    }



}
